package com.mianle.userapi.util;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期工具类
 *
 */
public class DateUtil {

    /**
     * yyyy-MM-dd HH:mm:ss DATE_TIME_FMT
     */
    public static final String DATE_TIME_FMT = "yyyy-MM-dd HH:mm:ss";
    /**
     * yyyy-MM-dd HH:mm
     */
    public static final String DATE_FMT_HM = "yyyy-MM-dd HH:mm";
    /**
     * yyyy-MM-dd DATE_FMT
     */
    public static final String DATE_FMT = "yyyy-MM-dd";

    public static final String TIME_FMT = "HH:mm:ss"; // 时间
    /**
     * HH:mm
     */
    public static final String HH_MM = "HH:mm";


    /**
     * yyyyMMddHHmmss 24小时制
     */
    public static final String DATE_TIME_FMT_STYLE = "yyyyMMddHHmmss";



    /**
     * 获取当前Unix时间戳(秒)
     * @return curTime
     */
    public static int getCurUnixTime() {
        long curTime = System.currentTimeMillis();
        String timestamp = String.valueOf(curTime/1000);
        return Integer.valueOf(timestamp);
    }


    /**
     * 获取指定的日志是星期几
     *
     * @param date
     * @return
     */
    public static int getWeekDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        return dayOfWeek;
    }

    /***************************************************************************
     * 判断currDate是否在startdate与enddate之间
     *
     * @param currDate
     *            当前日期
     * @param startdate
     *            日期范围开始
     * @param enddate
     *            日期范围截止
     * @return
     */
    public static boolean isInMiddle(Date currDate, Date startdate, Date enddate) {
        boolean result = false;
        if (null == currDate || null == startdate || null == enddate) {
            return result;
        }
        long currentTimeVal = currDate.getTime();
        result = ((currentTimeVal >= startdate.getTime()) && (currentTimeVal < enddate
                .getTime()));
        return result;
    }

    /**
     * 获取指定的日志是星期几
     *
     * @param date
     * @return
     */
    public static String getWeekDayString(Date date) {
        String weekString = "";
        final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
                "星期六" };
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        weekString = dayNames[dayOfWeek - 1];
        return weekString;
    }

    /**
     * 按照年月日 星期几的格式输入某天的日期
     *
     * @param date
     * @return
     */
    public static String getDayString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String tdate = sdf.format(date);
        tdate += " " + getWeekDayString(date);
        return tdate;
    }

    /**
     * 按照年月日 星期几的格式输入某天的日期
     *
     * @param date
     * @return
     */
    public static String getDayFormat(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd号");
        String tdate = sdf.format(date);
        return tdate;
    }

    public static String amOrPm(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        int hour = Integer.valueOf(sdf.format(date));
        if (hour <= 12) {
            return "上午";
        } else {
            return "下午";
        }
    }

    /*
     * public static void main(String[] args) { Calendar c =
     * Calendar.getInstance(); for(int i=18;i<30;i++) { c.set(2012, 10, i);
     * System.out.println(getWeekDayString(c.getTime())); } c.set(2012, 10, 16);
     * System.out.println(getDayString(c.getTime()));
     * System.out.println(amOrPm(new Date())); }
     */

    // 返回日期型yyyy-MM-dd
    public static Date getDateTimeByStr(String date) {
        Date date1 = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FMT);
            if (date != null)
                date1 = sdf.parse(date);
        } catch (Exception e) {
            System.out.println("将字符串转换成yyyy-MM-dd日期出错");
            e.printStackTrace();
        }
        return date1;
    }

    // 返回字符串"yyyy-MM-dd"
    public static String getDateStrByDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }



    public static String getDateStrByDate(Date date, String dtFormat) {
        if (date == null)
            return "";
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
            return dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    public static Date getBeginDate(Date date,String inFormat,String outFormat){
        return getDateTimeByStr(getDateStrByDate(date,inFormat)+" 00:00:00",outFormat);
    }

    public static Date getEndDate(Date date,String inFormat,String outFormat){
        return getDateTimeByStr(getDateStrByDate(date,inFormat)+" 23:59:59",outFormat);
    }

    /**
     * 返回当前开始日期 格式如：yyyy-MM-dd 00:00:00
     * @param date
     * @return
     */
    public static Date getBeginDate(Date date){
        return getBeginDate(date,DATE_FMT, DATE_TIME_FMT);
    }

    /**
     * 返回当前结束日期 格式如：yyyy-MM-dd 23:59:59
     * @param date
     * @return
     */
    public static Date getEndDate(Date date){
        return getEndDate(date, DATE_FMT, DATE_TIME_FMT);
    }


    public static Date getDateTimeByStr(String date, String dtFormat) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
            return dateFormat.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 返回最小日期<br>
     * 注：<br>
     * 第一个参数为空，返回第二个参数值<br>
     * 第二个参数为空，返回第一个参数值
     * @param date1
     * @param date2
     * @return
     */
    public static Date minDate(Date date1,Date date2){
        if(date1 == null)return date2;
        if(date2 == null)return date1;
        return date1.before(date2)?date1:date2;
    }

    /**
     * 返回最大日期<br>
     * 注：<br>
     * 第一个参数为空，返回第二个参数值<br>
     * 第二个参数为空，返回第一个参数值
     * @param date1
     * @param date2
     * @return
     */
    public static Date maxDate(Date date1,Date date2){
        if(date1 == null)return date2;
        if(date2 == null)return date1;
        return date1.before(date2)?date2:date1;
    }

    /**
     * 获取指定格式的日期
     * @param date
     * @param format
     * @return
     */
    public static Date getDate(Date date,String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        try {
            Date date1 = formatter.parse(dateString);
            return date1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /** 获取日期 */
    public static Date getCurrentDateTime() {

        return getCurrentDateTime(DATE_TIME_FMT);
    }

    public static Date getCurrentDate(){

        return getCurrentDate(DATE_FMT);
    }

    public static Date getCurrentTime(){

        return getCurrentTime(TIME_FMT);
    }

    public static Date getCurrentDateTime(String fmt) {

        return  dateStrToDate(fmt,getCurrentDateTimeStr(fmt));
    }

    public static Date getCurrentDate(String fmt){

        return  dateStrToDate(fmt,getCurrentDateStr(fmt));
    }

    public static Date getCurrentTime(String fmt){

        return  dateStrToDate(fmt,getCurrentTimeStr(fmt));
    }

    public static String getCurrentDateTimeStr() {

        return getCurrentDateTimeStr(DATE_TIME_FMT);
    }

    public static String getCurrentTimeStr() {

        return getCurrentTimeStr(TIME_FMT);
    }

    public static String getCurrentDateStr() {

        return getCurrentDateStr(DATE_FMT);
    }

    public static String getCurrentDateTimeStr(String fmt) {

        String temp = new SimpleDateFormat(fmt).format(new Date());

        return temp;
    }

    public static String getCurrentTimeStr(String fmt) {

        String temp = new SimpleDateFormat(fmt).format(new Date());

        return temp;
    }

    public static String getCurrentDateStr(String fmt) {

        String temp = new SimpleDateFormat(fmt).format(new Date());

        return temp;
    }

    public static String dateToDateStr(Date date) {

        String temp = new SimpleDateFormat(DATE_TIME_FMT).format(date);

        return temp;
    }

    public static String dateToDateStr(String fmt, Date date) {

        String temp = new SimpleDateFormat(fmt).format(date);

        return temp;
    }

    /** 转换为日期对象 */
    public static Date dateStrToDate(String date) {
        Date temp = null;
        try {
            temp = new SimpleDateFormat(DATE_FMT).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return temp;
    }

    /** 字符串转换为日期时间格式对象 */
    public static Date dateStrToDatetime(String date) {
        if (date == null) {
            return null;
        }
        Date temp = null;
        try {
            temp = new SimpleDateFormat(DATE_TIME_FMT).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return temp;
    }

    public static Date dateStrToDate(String fmt, String date) {
        Date temp = null;
        try {
            temp = new SimpleDateFormat(fmt).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return temp;
    }

    /** 格式化日期 */
    public static String formatDateTime(Date date) {

        return formatDateTime(DATE_TIME_FMT,date);
    }

    public static String formatDateTime(String fmt, Date date) {
        if (isNull(fmt) || isNull(date)) {
            return null;
        }
        String temp = new SimpleDateFormat(fmt).format(date);

        return temp;
    }

    /**
     * 获取前一天日期
     * @return
     */
    public static Date getYesterday(){

        Calendar calendar =Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    public static String formatTime(Date date){
        return formatTime(TIME_FMT,date);
    }

    public static String formatTime(String fmt, Date date) {
        if (isNull(fmt) || isNull(date)) {
            return null;
        }
        String temp = new SimpleDateFormat(fmt).format(date);

        return temp;
    }

    public static String formatDate(Date date){
        return formatDate(DATE_FMT,date);
    }

    public static String formatDate(String fmt, Date date) {
        if (isNull(fmt) || isNull(date)) {
            return null;
        }
        String temp = new SimpleDateFormat(fmt).format(date);

        return temp;
    }

    /** 月的第一天 */
    public static Date getMonthFirstDay(int i) {
        // i = 0 是本月 1 上一月
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -i);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return c.getTime();
    }
    /** 月的最后一天 */
    public static Date getMonthLastDay(int i) {

        // i = 0 是本月 1 上一月
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -i);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.roll(Calendar.DAY_OF_MONTH, -1);

        return c.getTime();
    }


    /** 月的第一天时间戳毫秒 */
    public static long getMonthFirstTime() {
        Date monthFirstDay = getMonthFirstDay(0);
        return getBeginDate(monthFirstDay).getTime();
    }
    /** 月的最后一天时间戳毫秒 */
    public static long getMonthLastTime() {
        Date monthLastDay = getMonthLastDay(0);
        return getEndDate(monthLastDay).getTime();
    }

    /** 当天的开始时间戳毫秒*/
    public static long getcurrDayStartTime() {
        Date startTime = DateUtil.getBeginDate(DateUtil.getCurrentDateTime());
        return startTime.getTime();
    }
    /** 当天的结束时间戳毫秒*/
    public static long getcurrDayEndTime() {
        Date endTime = DateUtil.getEndDate(DateUtil.getCurrentDateTime());
        return endTime.getTime();
    }

    public static long getStartTimeByDate(long currTime){
        Date date = new Date(currTime);
        Date startTime = DateUtil.getBeginDate(date);
        return startTime.getTime();
    }

    public static long getEndTimeByDate(long currTime){
        Date date = new Date(currTime);
        Date endTime = DateUtil.getEndDate(date);
        return endTime.getTime();
    }

    public static String getMonthFirstDay(String date) {
        Calendar c = Calendar.getInstance();
        c.setTime(dateStrToDate(date));
        c.set(Calendar.DAY_OF_MONTH,1);
        Date firstDate = c.getTime();
        return formatDate(DATE_FMT,firstDate);
    }

    public static String getMonthLastDay(String date) {
        Calendar c = Calendar.getInstance();
        c.setTime(dateStrToDate(date));
        c.set(Calendar.DAY_OF_MONTH,1);
        c.add(Calendar.MONTH,1);
        c.add(Calendar.DAY_OF_MONTH,-1);
        Date lastDate = c.getTime();
        return formatDate(DATE_FMT,lastDate);
    }

    /**
     * 获取当前时间字符串  如:201212061456123
     * @return
     */
    public static String get14CurrentDateTimeStr() {
        Calendar currTime = Calendar.getInstance();
        String year = String.valueOf(currTime.get(Calendar.YEAR));
        String month = String.valueOf(currTime.get(Calendar.MONTH) + 1);
        String day = String.valueOf(currTime.get(Calendar.DATE));
        String hour = String.valueOf(currTime.get(Calendar.HOUR_OF_DAY));
        String minute = String.valueOf(currTime.get(Calendar.MINUTE));
        String second = String.valueOf(currTime.get(Calendar.SECOND));
        StringBuffer temp = new StringBuffer();

        temp.append(year);
        if (month.length() == 1)
            temp.append("0");
        temp.append(month);
        if (day.length() == 1) {
            temp.append("0");
        }
        temp.append(day);
        if (hour.length() == 1) {
            temp.append("0");
        }
        temp.append(hour);
        if (minute.length() == 1) {
            temp.append("0");
        }
        temp.append(minute);
        if (second.length() == 1) {
            temp.append("0");
        }
        temp.append(second);

        return temp.toString();
    }

    public static List<String> weekList(){
        int weekSize = 7;
        List<String> list = new ArrayList<String>(weekSize);
        Date cur = getCurrentDate();
        list.add(0, dateToDateStr("MM月dd日", cur)+" "+getWeekOfDate(cur));

        for (int i = 1; i < weekSize; i++) {
            cur = addDate(cur, 1);
            list.add(i, dateToDateStr("MM月dd日", cur)+" "+getWeekOfDate(cur));
        }
        return list;
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static List<String> monthList(){
        String currentDay = getCurrentDateStr();
        String monthLastDay = getMonthLastDay(currentDay);
        boolean isMonthLastDay =  currentDay.equalsIgnoreCase(monthLastDay);

        int weekSize = 4;
        if(isMonthLastDay){
            weekSize = 3;
        }
        List<String> list = new ArrayList<String>(weekSize);
        Date cur = getCurrentDate();
        list.add(0, dateToDateStr("yyyy年MM月", cur));

        for (int i = 1; i < weekSize; i++) {
            cur = addMonth(cur, 1);
            list.add(i, dateToDateStr("yyyy年MM月", cur));
        }
        return list;
    }


    public static List<String> getMonthList(Date begin, Date end) {
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy年MM月");
        List<String> monthList = new ArrayList<String>();
        Calendar calendarBegin = Calendar.getInstance();
        calendarBegin.setTime(begin);
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.setTime(end);
        int months = (calendarEnd.get(calendarEnd.YEAR) - calendarBegin.get(calendarEnd.YEAR)) * 12  + (calendarEnd.get(calendarEnd.MONTH) - calendarBegin.get(calendarEnd.MONTH));

        for (int i = 0; i <= months; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(begin);
            calendar.add(Calendar.MONTH, i);
            monthList.add(monthFormat.format(calendar.getTime()));
        }

        return monthList;
    }



    /**交换两个日期 */
    public static void changeDate(Date date1,Date date2){
        if (isNull(date1,date2)) {
            return;
        }
        //判断两个时间的大小
        Calendar c1 =Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2 =Calendar.getInstance();
        c2.setTime(date2);
        if (c1.after(c2)) {
            date1 = c2.getTime();
            date2 = c1.getTime();
        }
    }

    /** 比较两个日期相差的年数 */
    public static int compareYear(Date date1,Date date2){
        if (isNull(date1)||isNull(date2)) {
            return 0;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);

        if (c1.equals(c2)) {
            return 0;
        }

        if (c1.after(c2)) {
            Calendar temp = c1;
            c1 = c2;
            c2 = temp;
        }
        //计算差值
        int result = c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
        return result;
    }

    /** 比较两个日期相差的天数 */
    public static int compareDay(Date date1, Date date2) {
        if (date1 == null || date2 == null)
            return 0;

        Calendar d1 = Calendar.getInstance();
        d1.setTime(date1);
        Calendar d2 = Calendar.getInstance();
        d2.setTime(date2);
        if (d1.after(d2)) {
            Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }
        /*
         * 经过上面的处理，保证d2在d1之后
         * 下面这个days可能小于0，因为d2和d1可能不在同一年里，这样的话虽然d1的年份小，但其在一年中的"第几天"却可能比d2大。
         */
        int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
        int y2 = d2.get(Calendar.YEAR);
        if (d1.get(Calendar.YEAR) != y2) {//如果不在同一年
            d1 = (Calendar) d1.clone();
            do {
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                /*
                 * 给定此 Calendar 的时间值，返回指定日历字段可能拥有的最大值。
                 * 例如，在某些年份中，MONTH 字段的实际最大值是 12，而在希伯来日历系统的其他年份中，该字段的实际最大值是 13。
                 * DAY_OF_YEAR：闰年366？
                 */
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        }
        return days;

    }

    /** 比较两个日期相差的周数 */
    public static int compareWeek(Date date1,Date date2){
        if (isNull(date1)||isNull(date2)) {
            return 0;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);

        if (c1.equals(c2)) {
            return 0;
        }

        if (c1.after(c2)) {
            Calendar temp = c1;
            c1 = c2;
            c2 = temp;
        }
        //计算差值
        int result = c2.get(Calendar.WEEK_OF_MONTH)-c1.get(Calendar.WEEK_OF_MONTH);
        return result;
    }

    /** 比较两个日期相差的月数 */
    public static int compareMonth(Date date1, Date date2) {
        if (date1 == null || date2 == null)
            return 0;

        int iMonth = 0;
        int flag = 0;
        try {
            Calendar objCalendarDate1 = Calendar.getInstance();
            objCalendarDate1.setTime(date1);

            Calendar objCalendarDate2 = Calendar.getInstance();
            objCalendarDate2.setTime(date2);

            if (objCalendarDate2.equals(objCalendarDate1))
                return 0;
            if (objCalendarDate1.after(objCalendarDate2)) {
                Calendar temp = objCalendarDate1;
                objCalendarDate1 = objCalendarDate2;
                objCalendarDate2 = temp;
            }

            if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1.get(Calendar.YEAR))
                iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1.get(Calendar.YEAR))* 12 + objCalendarDate2.get(Calendar.MONTH) - flag)- objCalendarDate1.get(Calendar.MONTH);
            else
                iMonth = objCalendarDate2.get(Calendar.MONTH)- objCalendarDate1.get(Calendar.MONTH) - flag;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return iMonth;
    }

    /** 比较两个日期相差的秒数 */
    public static long compareTime(Date date1, Date date2) {
        if (date1 == null || date2 == null)
            return 0;

        Calendar c = Calendar.getInstance();

        c.setTime(date1);
        long l1 = c.getTimeInMillis();

        c.setTime(date2);
        long l2 = c.getTimeInMillis();

        return (l2 - l1) / 1000;
    }

    //设置时间
    private static Date addDateTime(Date date,int type,int num){
        if (date == null) {
            return null;
        }
        //初始化日历对象
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        //根据类型添加
        switch(type){
            case 1:		//添加年
                cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)+num);
                break;
            case 2:		//添加月
                cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+num);
                break;
            case 3:		//添加日
                cal.set(Calendar.DATE, cal.get(Calendar.DATE)+num);
                break;
            case 4:		//添加时
                cal.set(Calendar.HOUR, cal.get(Calendar.HOUR)+num);
                break;
            case 5:		//添加分
                cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE)+num);
                break;
            case 6:		//添加秒
                cal.set(Calendar.SECOND, cal.get(Calendar.SECOND)+num);
                break;
        }

        //返回操作结果
        return cal.getTime();
    }

    //设置日期时间
    private static Date setDateTime(Date date,int type,int num){
        if (date == null) {
            return null;
        }
        //初始化日历对象
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        //根据类型添加
        switch(type){
            case 1:		//添加年
                cal.set(Calendar.YEAR, num);
                break;
            case 2:		//添加月
                cal.set(Calendar.MONTH, num);
                break;
            case 3:		//添加日
                cal.set(Calendar.DATE, num);
                break;
            case 4:		//添加时
                cal.set(Calendar.HOUR_OF_DAY, num);
                break;
            case 5:		//添加分
                cal.set(Calendar.MINUTE, num);
                break;
            case 6:		//添加秒
                cal.set(Calendar.SECOND, num);
                break;
        }

        //返回操作结果
        return cal.getTime();
    }

    /**
     * 返回时分秒毫秒都是0的日期
     * @param date
     * @return
     */
    public static Date getDateForYMD(Date date){
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }

    /**
     * 格式化回复时间，如果时间刚过去的时间比较短就格式，刚刚，几分钟前之类的。
     * 长一点就格式  yyyy-MM-dd HH:mm:ss
     * @param replydate
     * @return
     */
    public static String formatReplyDatetime(Date replydate){
        String repliestimeString ;
        if (replydate == null){
            return "";
        }
        Date now = new Date();
        Double diffDay;
        long poor = compareTime(replydate, now);

        if (poor < 60){
            repliestimeString = "刚刚";
        }else if (poor >=  60 && poor < 3600 ){
            repliestimeString = (poor /  60) + "分钟前";
        }else if (poor >= (60 * 60) && poor < 86400 ){
            repliestimeString = (poor / 3600) + "小时前";
        }else {
            diffDay = ((double) poor / 86400);
            repliestimeString = "";
            //repliestimeString = DateConvertUtils.format(replydate, DATE_TIME_FMT);
            if (diffDay <= 2 ){
                String dateString = (diffDay <= 1 ? "昨天" : "前天");
                repliestimeString = repliestimeString.replace(repliestimeString.substring(0, repliestimeString.indexOf(" ")), dateString);
            }
        }
        return repliestimeString;
    }

    /** 设置年、月、日 */
    public static Date setYMD(Date date,int year,int month,int day){

        return setYear(setMonth(setDate(date,day),month),year);
    }
    public static Date setYear(Date date,int num){
        return setDateTime(date,1,num);
    }
    public static Date setMonth(Date date,int num){
        return setDateTime(date,2,num);
    }
    public static Date setDate(Date date,int num){
        return setDateTime(date,3,num);
    }
    /** 设置时、分、秒 */
    public static Date setHMS(Date date,int hour,int minute,int second){

        return setHour(setMinute(setSecond(date,second),minute),hour);
    }
    public static Date setHour(Date date,int num){
        return setDateTime(date,4,num);
    }
    public static Date setMinute(Date date,int num){
        return setDateTime(date,5,num);
    }
    public static Date setSecond(Date date,int num){
        return setDateTime(date,6,num);
    }

    /** 添加年、月、日、时、分、秒 */
    public static Date addYear(Date date,int num){
        return addDateTime(date,1,num);
    }
    public static Date addMonth(Date date,int num){
        return addDateTime(date,2,num);
    }
    public static Date addDate(Date date,int num){
        return addDateTime(date,3,num);
    }
    /** 添加年、月、日 */
    public static Date addYMD(Date date,int year,int month,int day){

        return addYear(addMonth(addDate(date,day),month),year);
    }
    public static Date addHour(Date date,int num){
        return addDateTime(date,4,num);
    }
    public static Date addMinute(Date date,int num){
        return addDateTime(date,5,num);
    }
    public static Date addSecond(Date date,int num){
        return addDateTime(date,6,num);
    }
    /** 添加时、分、秒 */
    public static Date addHMS(Date date,int hour,int minute,int second){

        return addHour(addMinute(addSecond(date,second),minute),hour);
    }

    /** 可以用于判断Object,String,Map,Collection,String,Array是否为空 */
    public static boolean isNull(Object value) {
        if (value == null) {
            return true;
        } else if(value instanceof String){
            if(((String)value).trim().replaceAll("\\s", "").equals("")){
                return true;
            }
        }else if(value instanceof Collection) {
            if(((Collection)value).isEmpty()){
                return true;
            }
        } else if(value.getClass().isArray()) {
            if(Array.getLength(value) == 0){
                return true;
            }
        } else if(value instanceof Map) {
            if(((Map)value).isEmpty()){
                return true;
            }
        }else {
            return false;
        }
        return false;
    }

    public static boolean isNull(Object value, Object... items){
        if (isNull(value) || isNull(items)) {
            return true;
        }
        for (Object item : items) {
            if (isNull(item)) {
                return true;
            }
        }
        return false;
    }

    // 返回字符串"yyyy-MM-dd HH:mm:ss"
    public static String getDateTimeStrByDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 返回当前日期所在这个月的第一天日期，格式：yyyy-MM-01 00:00:00
     * @param date
     * @return
     */
    public static Date getBeginDateForMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        return getBeginDate(calendar.getTime());
    }


    public static Date millisToDate(long millis) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(millis);
        return c.getTime();
    }

    /**
     * yyyyMMddHHmmss 20180418000756 24小时制 转时间戳
     * @param date
     * @return
     */
    public static int dateStrToSecond(String date) {
        Date temp = null;
        try {
            temp = new SimpleDateFormat(DATE_TIME_FMT_STYLE).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String timestamp = String.valueOf(temp.getTime()/1000);
        return Integer.valueOf(timestamp);
    }

    public static long dateStrToSecondLong(String date) {
        Date temp = null;
        try {
            temp = new SimpleDateFormat(DATE_TIME_FMT_STYLE).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return temp.getTime()/1000;
    }



    public static void main(String[] args) {
        //获取当月第一天和最后一天例子
        System.out.println(DateUtil.formatDate(DateUtil.DATE_FMT,DateUtil.getMonthFirstDay(0)));
        System.out.println(DateUtil.getBeginDate(DateUtil.getMonthFirstDay(0)));
        System.out.println(DateUtil.formatDate(DateUtil.DATE_FMT,DateUtil.getMonthFirstDay(0)));
        System.out.println(DateUtil.formatDate(DateUtil.DATE_FMT,DateUtil.getMonthLastDay(0)));


        System.out.println(DateUtil.dateStrToSecond("20180418000756"));


    }

}

